package com.gilgamesh.mall.product.controller;

import com.gilgamesh.mall.product.service.ClassficService;
import com.gilgamesh.mall.sdk.bean.product.ClassficEntity;
import com.gilgamesh.mall.sdk.client.product.ClassficClient;
import com.gilgamesh.mall.sdk.dto.ResponseDTO;
import com.gilgamesh.mall.sdk.dto.product.ClassficDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * @author yanm
 * @date 2021/1/11 14:08
 **/
@RestController
@RequestMapping("api/pdt")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@CrossOrigin
public class ClassficController implements ClassficClient {

    /**
     * 分类服务注入
     **/
    private final ClassficService classficService;


    /**
     * 新增一个分类
     *
     * @param classficEntity xxx
     * @return 标准返回
     * @author yanm
     * @date 2021/1/11 14:31
     **/
    @Override
    @PostMapping("addClassfic")
    public ResponseDTO<Integer> addClassfic(@RequestBody ClassficEntity classficEntity) {

        return classficService.addClassfic(classficEntity);
    }

    @PostMapping("addClassficByParams")
    public ResponseDTO<Integer> addClassfic(@RequestParam(value = "classficName" ,required = false)
                                                        String classificationName,
                                            @RequestParam(value = "level" ,required = false) Byte level,
                                            @RequestParam(value = "superiorId" ,required = false) Long pid,
                                            @RequestParam(value = "rank" ,required = false) Byte rank) {
        ClassficEntity classficEntity = new ClassficEntity();
        classficEntity.setRank(rank);
        classficEntity.setCreateTime(new Date());
        classficEntity.setSuperiorId(pid);
        classficEntity.setClassficName(classificationName);
        classficEntity.setLevel(level);
        System.out.println("接口addClassficByParams  = "+classficEntity.toString());
        return classficService.addClassfic(classficEntity);
    }

    @Override
    @PostMapping("treeClassfic")
    public ResponseDTO<ClassficDTO> getTreeList(@RequestParam Long classficId){
        System.out.println("treeClassfic接口 = "+classficId);
        return classficService.listTreeById(classficId);
    }

    @PostMapping("treeClassficW")
    public ResponseDTO<List<ClassficDTO>> getTreeListW(){
        System.out.println("treeClassficW接口");
        return classficService.listTree();
    }

    @Override
    @PostMapping("listClassfic")
    public ResponseDTO<List<ClassficDTO>> listClassfic(@RequestBody ClassficEntity classficEntity){
        System.out.println("listClassfic接口 = "+classficEntity.toString());
        return classficService.listClassfics(classficEntity);
    }

    @PostMapping("listClassficByLS")
    public ResponseDTO<List<ClassficDTO>> listClassfic(@RequestParam(value = "level",required = false) Byte level,
                                                       @RequestParam(value = "pid" ,required = false)Long pid){

        ClassficEntity entity = new ClassficEntity();
        if(level!=null){
            entity.setLevel(level);
        }
        if(pid!=null){
            entity.setSuperiorId(pid);
        }
        System.out.println("listClassficByLS接口入参 = "+entity.toString());
        return classficService.listClassfics(entity);

    }

    @Override
    @GetMapping("getClassfic/{id}")
    public ResponseDTO<ClassficDTO> getById(@PathVariable(value = "id") Long id){
        return classficService.getClassficById(id);
    }


}
